package com.green.javagreen.controller;

import com.green.javagreen.service.UserService;
import com.green.javagreen.bean.ResultBean;
import com.green.javagreen.bean.UserBean;
import com.green.javagreen.injections.PermissionCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 */
@Api(value = "用户controller")
@RestController
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @ApiOperation(value = "获取列表")
    @PostMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean list(@RequestBody UserBean userBean) {
        Page<UserBean> page = userService.find(userBean);
        return ResultBean.returnPage(page);
    }

    @ApiOperation(value = "获取单个用户")
    @GetMapping("/{id}")
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean get(@PathVariable int id) {
        UserBean page = userService.get(id);
        return ResultBean.returnSuccess(page);
    }

    @ApiOperation(value = "保存 新增/修改")
    @PutMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean save(@RequestBody UserBean userBean) {
        userService.save(userBean);
        return ResultBean.returnSuccess(null);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean delete(@PathVariable int id) {
        userService.delete(id);
        return ResultBean.returnSuccess(null);
    }


}
